package com.wujilin.okra.adapter;

import static com.wujilin.okra.Okra.getLayoutInflater;
import android.content.Context;
import android.database.Cursor;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

import com.wujilin.okra.R;
import com.wujilin.okra.database.ModelCursor;
import com.wujilin.okra.model.Model;

/**
 * @author Belin Wu
 */
public class OkraListAdapter extends CursorAdapter {
	private String text;
	
    public OkraListAdapter(Context context, Cursor cursor) {
        super(context, cursor, 0);
    }

    public OkraListAdapter(Context context, Cursor cursor, String text) {
    	super(context, cursor, 0);
    	this.text = text;
    }

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		return getLayoutInflater(context).inflate(R.layout.list_item_okra, parent, false);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		TextView contentText = (TextView) view.findViewById(R.id.objective_content);
        TextView timeText = (TextView) view.findViewById(R.id.objective_time);
        TextView gradeText = (TextView) view.findViewById(R.id.objective_grade);
        TextView categoryText = (TextView) view.findViewById(R.id.objective_category);
        
        Model model = ((ModelCursor) cursor).getModel();
        timeText.setText(model.getString("year") + "/Q" + model.getString("quarter"));
        gradeText.setText(model.getString("grade"));
        categoryText.setText(model.getString("category"));
        String content = model.getString("content");
        if (text != null && text.length() > 0) {
        	Spanned spanned = Html.fromHtml(content.replaceAll(text, "<font color='#46BF18'>" + text + "</font>"));
        	contentText.setText(spanned);
        } else {
        	contentText.setText(content);
        }
	}
}
